#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, M = 2e5 + 10;
int idx;
int h[N],ne[M],e[M],w[M];

int dis[N];
bool state[N];

void add(int a,int b,int c)
{
    e[idx] = b;
    ne[idx] = h[a];
    w[idx] = c;
    h[a] = idx ++;
}

void dij(int st)
{
    memset(dis,0x3f,sizeof dis);
    priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>> >heap;
    heap.push({0,st});
    dis[st] = 0;
    while(heap.size())
    {
        auto nodex = heap.top();
        heap.pop();
        int disx = nodex.first;
        int node = nodex.second;
        if(state[node] == 0) state[node] = 1;
        else continue;
        for(int i = h[node]; i != -1; i = ne[i])
        {
            int j = e[i];
            if(dis[j] > disx + w[i])
            {
                dis[j] = disx + w[i];
                heap.push({dis[j],j});
            }
        } 
    }
}

int main()
{
    memset(h,-1,sizeof h);
    int n,m,st;
    cin >> n >> m >> st;
    for(int i = 1; i <= m; i ++)
    {
        int a,b,c;
        cin >> a >> b >> c;
        add(a,b,c);
    }
    dij(st);
    for(int i = 1; i <= n; i ++)
    {
        cout << dis[i] << ' ';
    }
    
}   